package com.apes.enquiry.product.model;

import com.apes.enquiry.brand.model.SpBrand;
import com.apes.enquiry.product.repository.AccumulatorProductRepository;
import com.apes.enquiry.product.repository.OilyProductRepository;
import com.apes.enquiry.product.repository.TyreProductRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.audit.Auditing;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：商品列表（供应商询价平台）
 *
 * @ClassName product
 * @Description 描述
 * @Author wushilong
 * @Date 2022/10/13 14:46
 * @Version 1.0
 */
@Auditing
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SpProduct.CONST_TABLE_NAME, indexes = {@Index(columnList = "name")})
public class SpProduct extends Model {

    public static final String CONST_TABLE_NAME = "sp_mas_product";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商品列表", padding = 9)
    @Comment("编码")
    private String id; //商品编码

    @Column(name = "name", nullable = false, length = 50)
    @Comment("商品名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商")
    private PartyRole supplier;

    @Column(name = "type", nullable = false, length = 24)
    @Comment("商品分类")
    private String type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    @Comment("品牌")
    private SpBrand spBrand;

    @Column(name = "quality", length = 24)
    @Comment("品质")
    private String quality;

    @Column(name = "pn", nullable = false, length = 24)
    @Comment("零件号")
    private String OE;

    @Column(name = "merchant_product", length = 150)
    @Comment("商家商品编码")
    private String merchantProduct;

    @Column(name = "not_tax_unit_price", nullable = false, columnDefinition = "NUMBER(16,2) DEFAULT 0")
    @Comment("不含税单价")
    private Double notTaxUnitPrice = 0.0;

    @Column(name = "merchant_stock_qty")
    @Comment("库存数量")
    private Integer merchantStockQty;

    @Column(name = "mate_state", nullable = false, length = 24)
    @Comment("匹配状态")
    private String mateState;//(underway:匹配中/ fail：匹配失败/ awaitMaintain:待维护品牌件关系/ succeed:匹配成功/ notNeed:无需匹配)

    @Column(name = "putaway_state", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("上架状态")
    private boolean putawayState;//（0：下架  1：上架）默认下架

    @Column(name = "standard_type", length = 90)
    @Comment("标准商品类型")
    private String standardType;//（原厂标准商品:Whole /品牌标准商品：brandWhole /蓄电池：Accumulator /轮胎：Tyre /机油：Oily /改装商品:refit）

    @Column(name = "standard_id", length = 24)
    @Comment("标准商品ID")
    private String standardId;

    @Column(name = "product_state", nullable = false, length = 24)
    @Comment("商品状态")
    private String productState;//(review:审核中/notOffer:无法系统报价/awaitMaintain:待维护品牌件关系/offer:可系统报价/awaitPutaway:待上架/history:历史报价)

    @Column(name = "describe", length = 150)
    @Comment("产品描述")
    private String describe;

    @Column(name = "temporary", columnDefinition = "number(1,0) default 0")
    @Comment("临时商品标记")
    private Boolean temporary;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "automobile_id", referencedColumnName = "id")
    @Comment("汽车品牌")
    private AutomobileBrand automobile;
    @Transient
    private String automobileName;

    public  String getAutomobileName(){
        if (Objects.nonNull(this.automobile)) {
            return automobile.getAutomobileBrand();
        }
        return this.automobileName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    @Comment("标准名称")
    private Category category;  //标准名称


    @Column(name = "place_of_origin", length = 90)
    @Comment("产地")
    private String placeOfOrigin;

    @Column(name = "guarantee_period", length = 90)
    @Comment("质保期")
    private String guaranteePeriod;

    @Transient
    private String dis;

    @Transient
    private Map<String,String> transientCategory;

    public String getDis() {
        return dis;
    }
    //(review:审核中/notOffer:无法系统报价/awaitMaintain:待维护品牌件关系/offer:可系统报价/awaitPutaway:待上架)
    public void setDis(String dis) {
        switch (this.productState){
            case "review":
                this.dis="审核中";
                break;
            case "notOffer":
                this.dis="无法系统报价";
                break;
            case "awaitMaintain":
                this.dis="待维护品牌件关系";
                break;
            case "offer":
                this.dis="可系统报价";
                break;
            case "awaitPutaway":
                this.dis="待上架";
                break;
        }
    }

    @Transient
    private String qualityName;

        public String getQualityName() {
            if (Objects.nonNull(this.spBrand)){
                return spBrand.getQualityName();
            }
            return this.qualityName;
    }

    public void setQualityName(String qualityName) {
        this.qualityName = qualityName;
    }

    @Transient
    private String subdivideBrandName;

    public  String getSubdivideBrandName(){
        if (Objects.nonNull(this.spBrand)){
            return spBrand.getSubdivideBrandName();
        }
        return this.subdivideBrandName;
    }


    public boolean offer(){
        return "offer".equals(this.productState);
    }

    public boolean notTaxUnitPrice(){
        return !(notTaxUnitPrice <= 0.0);
    }

    public boolean merchantStockQty(){
        return !(merchantStockQty <= 0);
    }

    //获取扩展属性（注意类型）
    public <T> T extend(){
        switch (this.standardType){//（原厂标准商品:Whole /品牌标准商品：brandWhole /蓄电池：Accumulator /轮胎：Tyre /机油：Oily）
            case "Accumulator":
                return (T)SpringManager.getBean(AccumulatorProductRepository.class).findBySpId(this.id);
            case "Tyre":
                return (T)SpringManager.getBean(TyreProductRepository.class).findBySpId(this.id);
            case "Oily":
                return (T)SpringManager.getBean(OilyProductRepository.class).findBySpId(this.id);
        }
        return (T)this;
    }
}
